package com.xie.web;

import com.xie.domain.Account;
import com.xie.domain.Student;
import com.xie.service.AccountService;
import com.xie.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 本间芽衣子
 * @author As long as you are on the road, there is no place you can’t reach。
 * @program: ssm  表现层
 * @date 2021年04月19日 9:08
 */

@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("/findAccount")
    public String findAccount(Model model){
        System.out.println("执行了查询账户");
//        List<Account> account = accountService.findAccount();
//        model.addAttribute("account",account);
        return "findAlAccount";
    }


    @RequestMapping("/update")
    private String updave(){
        return "update";
    }

    /**
     * 保存
     */
    @RequestMapping("/saveAnimalInfo")
    public String saveAnimalInfo(Account account,Model model){
         accountService.saveAccount(account);
        List<Account> account1 = accountService.findAccount();
        model.addAttribute("account1",account1);
        return "findAlAccount";
    }
    /**
     * 新增，查询
     * @param account
     * @param model
     * @return
     */
    @RequestMapping("/saveAccount")
    public String saveAccount(Account account,Model model){
        System.out.println("执行了查询账户");
        accountService.saveAccount(account);
        List<Account> account1 = accountService.findAccount();
        model.addAttribute("account1",account1);
        return "findAlAccount";
    }

    /**
     * 删除
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/deleteUser/{id}")
    public String deleteAnimalInfo(@PathVariable("id") int id,Model model){
        accountService.deleteAccount(id);
        List<Account> account1 = accountService.findAccount();
        model.addAttribute("account1",account1);
        return "findAlAccount";
    }


    /**
     * 登入
     * @param student
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/userAll")
    private String userAll(Student student, HttpSession session, Model model){
        Student student1 = studentService.findStudent(student.getUsername(), student.getPassword());
        List<Account> account1 = accountService.findAccount();
        model.addAttribute("account1",account1);
        return "findAlAccount";
    }

    /**
     * 查询
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/update/{id}")
    public String update(Model model,@PathVariable("id") int id){
        model.addAttribute("account",accountService.getAccount(id));
        return "up";
    }

    /**
     * 修改
     * @param account
     * @param model
     * @return
     */
    @RequestMapping("/modifyAccount")
    public String modifyAccount(Account account,Model model){
       accountService.modifyAccount(account.getId(),account.getName(),account.getMoney());
        List<Account> account1 = accountService.findAccount();
        model.addAttribute("account1",account1);
        return "findAlAccount";
    }
}
